package com.aaa.system.service.imp;

import com.aaa.entity.DictData;
import com.aaa.entity.DictType;
import com.aaa.entity.User;
import com.aaa.system.mapper.DictDataMapper;
import com.aaa.system.mapper.DictTypeMapper;
import com.aaa.system.mapper.UserMapper;
import com.aaa.system.service.DictTypeService;
import com.aaa.system.vo.DictTypeVo;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DictTypeServiceImp implements DictTypeService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<String> getAllDictList() {
        //查询所有的数据字典类型
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        List<DictType> dictTypes = dictTypeMapper.selectList(wrapper);
        //查询所有类型字典的数据
        for(DictType dictType:dictTypes){
            QueryWrapper<DictData> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("status",0);
            wrapper1.eq("dict_type",dictType.getDictType());
            List<DictData> dictData = dictDataMapper.selectList(wrapper1);
            redisTemplate.opsForValue().set(dictType.getDictType(), JSON.toJSONString(dictData));
        }
        return new Result<String>(200,"缓存同步成功");
    }

    @Override
    public Result<IPage<DictType>> getDictTypeList(DictTypeVo dictTypeVo) {
        IPage page = new Page(dictTypeVo.getCurrent(),dictTypeVo.getSize());
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dictTypeVo.getDictName())){
            wrapper.like("dict_name",dictTypeVo.getDictName());
        }
        if (StringUtils.isNotBlank(dictTypeVo.getDictType())){
            wrapper.like("dict_type",dictTypeVo.getDictType());
        }
        if (Objects.nonNull(dictTypeVo.getStatus())){
            wrapper.like("status",dictTypeVo.getStatus());
        }
        if (Objects.nonNull(dictTypeVo.getDateRange())&&dictTypeVo.getDateRange().length==2){
            wrapper.between("create_time",dictTypeVo.getDateRange()[0],dictTypeVo.getDateRange()[1]);
        }
        IPage page1 = dictTypeMapper.selectPage(page, wrapper);
        return new Result<>(200,"查询成功",page1);
    }

    @Override
    public Result<List<DictType>> getAllDict() {
        QueryWrapper<DictType> wrapper =new QueryWrapper<>();
        wrapper.eq("status",0);
        List<DictType> typeList = dictTypeMapper.selectList(wrapper);
        return new Result<>(200,"查询成功",typeList);
    }

    @Transactional
    @Override
    public Result<Boolean> saveOrUpdDictType(DictType dictType) {
        String token = WebUtil.getRequest().getHeader("token");
        String username = TokenUtils.getInfo(token).get("username").toString();
        //根据手机号进行查询
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",username);
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        User userdata = userMapper.selectOne(queryWrapper);
        //如果有Id 为修改  否则为添加
        if (Objects.nonNull(dictType.getDictId())){
            //修改的同时也要修改数据表中的类型
            QueryWrapper<DictType> wrapper = new QueryWrapper<>();
            if (Objects.nonNull(dictType.getDictId())){
                wrapper.eq("dict_id",dictType.getDictId());
            }
            DictType dictType1 = dictTypeMapper.selectOne(wrapper);
            //修改前的类型
            String beforeType = dictType1.getDictType();
            //修改后的数据
            String afterType = dictType.getDictType();
            if (beforeType.equals(afterType)){
                dictType.setUpdateBy(userdata.getUserName());
                dictType.setUpdateTime(LocalDateTime.now());
                int i = dictTypeMapper.updateById(dictType);
                return new Result<>(200,"修改成功",i>0?true:false);
            }
            //根据类型修改类型
            dictDataMapper.updtypeBytype(beforeType,afterType);
            //修改
            dictType.setUpdateBy(userdata.getUserName());
            dictType.setUpdateTime(LocalDateTime.now());
            dictTypeMapper.updateById(dictType);
            return new Result<>(200,"修改成功");
        }
        //添加
        dictType.setCreateBy(userdata.getUserName());
        dictType.setCreateTime(LocalDateTime.now());
        int i = dictTypeMapper.insert(dictType);
        return new Result<>(200,"添加成功",i>0?true:false);
    }

    @Override
    public Result<Boolean> delDictType(Long[] dictIds) {
        int i = dictTypeMapper.delDictType(dictIds);
        return new Result<>(200,"删除成功",i>0?true:false);
    }
}
